package com.ayoka.ui.main;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import com.ayoka.R;
import com.ayoka.base.BaseActivity;
import com.ayoka.base.IOnBackPressed;
import com.ayoka.bean.UserInformation;
import com.ayoka.databinding.ActivityMainBinding;
import com.ayoka.mvp.MainMVP.IMainView;
import com.ayoka.mvp.MainMVP.MainPresenter;
import com.ayoka.ui.launch.VerifyMobileActivity;
import com.ayoka.utils.PushUtil;
import com.jcp.baselibrary.dialog.DialogHelper;
import com.jcp.baselibrary.utils.StatusBarUtils;
import com.jcp.baselibrary.utils.UIUtils;
import com.tencent.TIMCallBack;
import com.tencent.TIMConversationType;
import com.tencent.TIMManager;
import com.tencent.TIMUserStatusListener;

/**
 * =========================================================
 * @ProjectName:
 * @ClassName: MainActivity
 * @Description: 主页面
 * @Author: jincp
 * @CreateDate: 2019/3/14 13:53
 * =========================================================
 */
public class MainActivity extends BaseActivity<ActivityMainBinding, MainPresenter> implements IMainView, TIMCallBack {


    public static void start(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }
    @Override
    protected void onBeforeSetContentLayout() {
        StatusBarUtils.setStatusBar(this, false, false);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        configFragments();
        mPresenter.loginTIM(MainActivity.this);

        if (!UserInformation.getInstance().hasMobile()){
            DialogHelper.getOkCancelDialog(this, "提示", getString(R.string.not_yet_bound_mobile), getString(R.string.cancel), getString(R.string.binding_mobile), true, null, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    VerifyMobileActivity.start(MainActivity.this);
                }
            }).show();
        }
    }


    @Override
    protected MainPresenter getPresenter() {
        return new MainPresenter();
    }

    /**
     * @Date create date : 2017/3/22 15:06
     * @author admin
     * @Description 初始化fragment
     */
    public void configFragments() {
        FragmentManager manager = getSupportFragmentManager();
        NavigationFragment mNavBar = ((NavigationFragment) manager.findFragmentById(R.id.fragment_navigation));
        mNavBar.setup(manager, R.id.frameLayout);
    }

    @Override
    public void onError(int i, String s) {
        Log.d(TAG, s);
        switch (i) {
            case 6208:
                //离线状态下被其他终端踢下线
                DialogHelper.getOkCancelDialog(this, "下线通知", UIUtils.getString(R.string.kick_logout),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mPresenter.loginTIM(MainActivity.this);
                            }
                        }).show();
                break;
            case 6200:
                showToast(UIUtils.getString(R.string.login_error_timeout));
                break;
            default:
                showToast(UIUtils.getString(R.string.login_error));
                break;
        }
    }

    @Override
    public void onSuccess() {
        //初始化程序后台后消息推送
        PushUtil.getInstance();
        //互踢下线逻辑
        TIMManager.getInstance().setUserStatusListener(new TIMUserStatusListener() {
            @Override
            public void onForceOffline() {
                DialogHelper.getOkDialog(MainActivity.this, "温馨提示", getString(R.string.kick_logout), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        UserInformation.setUserInformation(null);
                        finish();
                    }
                }).show();
            }

            @Override
            public void onUserSigExpired() {
                //票据过期，需要重新登录
                DialogHelper.getOkDialog(MainActivity.this, "温馨提示", getString(R.string.kick_logout), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        UserInformation.setUserInformation(null);
                        finish();
                    }
                }).show();
            }
        });
    }

    @Override
    public void onBackPressed() {
        exit();
//        hide();
    }

    private void hide() {
        // 获取当前的fragment
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameLayout);
        // 判断当前fragment是否可以点击返回按钮
        if ((fragment instanceof IOnBackPressed) && ((IOnBackPressed) fragment).onBackPressed()) {
            // 设置隐藏
            Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
            mHomeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mHomeIntent.addCategory(Intent.CATEGORY_HOME);
            startActivity(mHomeIntent);
        }
    }

    //声明一个long类型变量：用于存放上一点击“返回键”的时刻
    private long mExitTime;
    private void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            //大于2000ms则认为是误操作，使用Toast进行提示
            showToast("再按一次退出程序");
            //并记录下本次点击“返回键”的时刻，以便下次进行判断
            mExitTime = System.currentTimeMillis();
        } else {
            //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
            finish();
            System.exit(0);
        }
    }
}
